##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################

from PyQt4 import QtCore
from PyQt4 import QtGui
from lisp.ui.preferences.preferences_widget import PreferencesWidget
import math


class Pitch(PreferencesWidget):

    NAME = 'Pitch - Speed'

    def __init__(self, size, parent=None):
        super(Pitch, self).__init__(size, parent)

        self.groupBox_10 = QtGui.QGroupBox(self)

        self.verticalLayoutWidget_3 = QtGui.QWidget(self.groupBox_10)
        self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(0, 20, 431, 121))
        self.verticalLayout_3 = QtGui.QVBoxLayout(self.verticalLayoutWidget_3)
        self.verticalLayout_3.setMargin(0)

        self.horizontalLayoutWidget_4 = QtGui.QWidget(self.groupBox_10)
        self.horizontalLayoutWidget_4.setGeometry(QtCore.QRect(0, 20, 431, 60))
        self.horizontalLayout_4 = QtGui.QHBoxLayout(self.horizontalLayoutWidget_4)
        self.horizontalLayout_4.setMargin(0)

        self.horizontalSlider = QtGui.QSlider(self.horizontalLayoutWidget_4)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        self.horizontalSlider.setSizePolicy(sizePolicy)
        self.horizontalSlider.setMinimum(1)
        self.horizontalSlider.setMaximum(100)
        self.horizontalSlider.setPageStep(1)
        self.horizontalSlider.setProperty("value", 10)
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setTickPosition(QtGui.QSlider.TicksAbove)
        self.horizontalSlider.setTickInterval(10)
        self.horizontalLayout_4.addWidget(self.horizontalSlider)

        self.label_7 = QtGui.QLabel(self.horizontalLayoutWidget_4)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
        self.label_7.setSizePolicy(sizePolicy)
        self.label_7.setAlignment(QtCore.Qt.AlignCenter)
        self.horizontalLayout_4.addWidget(self.label_7)

        self.verticalLayout_3.addWidget(self.horizontalLayoutWidget_4)

        self.horizontalLayoutWidget = QtGui.QWidget(self.groupBox_10)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 20, 431, 60))
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setMargin(0)

        self.horizontalSlider_2 = QtGui.QSlider(self.horizontalLayoutWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        self.horizontalSlider_2.setSizePolicy(sizePolicy)
        self.horizontalSlider_2.setMinimum(-12)
        self.horizontalSlider_2.setMaximum(12)
        self.horizontalSlider_2.setPageStep(1)
        self.horizontalSlider_2.setProperty("value", 0)
        self.horizontalSlider_2.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider_2.setTickPosition(QtGui.QSlider.TicksAbove)
        self.horizontalSlider_2.setTickInterval(1)
        self.horizontalLayout.addWidget(self.horizontalSlider_2)

        self.label = QtGui.QLabel(self.horizontalLayoutWidget)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)
        self.label.setSizePolicy(sizePolicy)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.horizontalLayout.addWidget(self.label)

        self.verticalLayout_3.addWidget(self.horizontalLayoutWidget)

        self.horizontalSlider.valueChanged.connect(self.speedChanged)
        self.horizontalSlider_2.valueChanged.connect(self.pitchChanged)

        self.retranslateUi()

    def retranslateUi(self):
        self.groupBox_10.setTitle("Speed - Pitch")
        self.label_7.setText("speed x1.0")
        self.label.setText("+0 semitones")

    def enableCheck(self, enable):
        self.groupBox_10.setCheckable(enable)
        self.groupBox_10.setChecked(False)

    def getConfiguration(self):
        conf = {}
        if(not (self.groupBox_10.isCheckable() and not self.groupBox_10.isChecked())):
            conf['pitch'] = {'tempo': self.horizontalSlider.value() / 10, 'pitch': math.pow(2, self.horizontalSlider_2.value() / 12)}
        return conf

    def setConfiguration(self, conf):
        if(conf is not None):
            if('pitch' in conf):
                if(type(conf['pitch']) == type({})):
                    if('tempo' in conf['pitch']):
                        self.horizontalSlider.setValue(conf['pitch']['tempo'] * 10)
                    else:
                        self.horizontalSlider.setEnabled(False)
                    if('pitch' in conf['pitch']):
                        self.horizontalSlider_2.setValue(round(12 * math.log(conf['pitch']['pitch'], 2)))
                    else:
                        self.horizontalSlider_2.setEnabled(False)
                else:
                    self.setEnabled(False)
            else:
                self.setEnabled(False)

    def speedChanged(self, value):
        self.label_7.setText('speed x' + str(value / 10.0))

    def pitchChanged(self, value):
        if value < 0:
            self.label.setText(str(value) + ' semitones')
        else:
            self.label.setText('+' + str(value) + ' semitones')
